<?

/** Filtering class. Filters the outgoing and incoming data.
 *
 *
 *  This code is licensed under GPL2.
 *  See LICENSE or www.gnu.org for more details.
 *
 *  @author Jyry Kuukkanen
 *
 *  $Id: dzfilter.php 595 2006-04-03 16:00:47Z jyry $
 */

include_once("sostring.php");
include_once("sosqlhi.php");
include_once("soformat.php");

include_once("dztemplate.php");
include_once("dzpurkki.php");
include_once("dzlaituri.php");

/** Filters values out to client or back from it.
 *  This class takes the data from the TISKI (dzPurkki) and copies it
 *  to TULLI data store in dzPurkki - or the other way round.
 *  Before copying the values are converted acording to the data type and
 *  the column settings and the general application settings stored in
 *  ini files.
 *  In filtering (TULLI to TISKI):
 *  Copies data from TULLI back to TISKI converting the data to the
 *  internal format.
 *  out filtering: (TISKI to TULLI):
 *  Copies data from TISKI to TULLI and formats the data for user.
 *  @var dzTemplate $__Template Column structure information
 *  @var dzLaituri $__Laituri Data store
 */
class dzFilter{

    var $__Template;
    var $__Laituri;

    /** Constructor
     *  @param dzTemplate $Template Column structure information
     *  @param dzLaituri $Laituri The actual data store
     */
     function dzFilter(&$Template, &$Laituri) {
        $this->__Template = &$Template;
        $this->__Laituri = &$Laituri;
     } // dzFilter


    /** Filter data from TULLI to TISKI
     *  Filtering the values entered by the user.
     */
    function filterIn() {
        $mode = $this->__Template->getViewData(DZ_VI_MODE);
        /* if mode is query just copy the TULLI to TISKI */
        if ($mode == DZM_QUERY) {
            $this->__Laituri->copy(TULLI, TISKI);
            return;     // <--EXIT!!
        } // if $mode

        $this->__Laituri->initStore(TISKI);
        $cols = $this->__Laituri->getColNames(TULLI);

        /* loop through all columns */
        foreach ($cols as $col) {
            $values = $this->__Laituri->getStoreCol(TULLI, $col);

            /* Conversion */
            $convert_type =
                $this->__Template->getColProperty(
                    $col, DZCPT_CONTROL, DZCP_CONVERT);
            if (!$convert_type) {
                /* get the field's data type */
                $type =
                    $this->__Template->getColProperty(
                        $col, DZCPT_STRUCT, DZCP_TYPE);
                if (in_array($type,
                    array(SODT_DATE, SODT_TIME, SODT_DATETIME,
                          SODT_DEC, SODT_INT))) {
                    $convert_type = $type;
                }; // if in_array()
            } // if $convert

            /* Is the component is check box. Convert the data to
            correct boolean value */
            $widget = $this->__Template->getColProperty(
                $col, DZCPT_CONTROL, DZCP_WIDGET);
            if ($widget == "checkbox") $convert_type = DZCV_BOOL_IN;

            /* Check if column formatting is needed */
            if ($convert_type) {
                /* The last parameter 0 means direction that is in */
                $this->__formatCol($col, $values, $convert_type, 1);
            } // if


            /* Hash=md5? */

#            $hash = $this->__Template->getColProperty(
#                $col, DZCPT_CONTROL, DZCP_HASH);
#            if ($hash == "md5") {
#                $old_vals = $this->__Laituri->getStoreCol(TAFFEL, $col);
#                $icount = count($values);
#                for ($i = 0; $i < $icount; $i++) {
#                    /* No hashing when the value has not been changed */
#                    if ($old_vals[$i] != $values[$i]) {
#                        $values[$i] = md5($values[$i]);
#                    }; // if
#                }; // for
#            }; // if


            $this->__Laituri->setStoreCol(TISKI, $col, $values);
        } // foreach
    } // filterIn


    /** Filter data from TISKI to TULLI
     */
    function filterOut() {
        $mode = $this->__Template->getViewData(DZ_VI_MODE);

        /* if mode is "query" and not "pseudo" form, then just copy
         * the TISKI to TULLI as is and EXIT */
        if (($mode == DZM_QUERY) && (!$this->__Template->isPseudo())) {
            $this->__Laituri->copy(TISKI, TULLI);
            return; // <-- EXIT!!
        } // if $mode

        $this->__Laituri->initStore(TULLI);
        $cols = $this->__Laituri->getColNames(TISKI);

        /* loop through all columns */
        foreach ($cols as $col) {
            $values = $this->__Laituri->getStoreCol(TISKI, $col);
            $type = $this->__Template->getColProperty(
                $col, DZCPT_STRUCT, DZCP_TYPE);
            /* Is the component is check box. Convert the data to
               correct boolean value */
            $widget = $this->__Template->getColProperty(
                $col, DZCPT_CONTROL, DZCP_WIDGET);
            if ($widget == "checkbox") $type = DZCV_BOOL_OUT;

            /* The last parameter 0 means directio that is out */
            $this->__formatCol($col, $values, $type, 0);

            /* Store the converted column values */
            $this->__Laituri->setStoreCol(TULLI, $col, $values);
        } // foreach

    } // filterOut


    /** Format a column values for output.
     *  Converts all the values in one column in specified manner.
     *  See the current possible conversions in dzconst.php. Conversion
     *  defines are prefixed with DZCV.
     *  @param string $ColName Column name
     *  @param array/str $ColArray Column values
     *  @param string $ConversionType What type of the conversion to perform.
     *  @param int $Dir Direction of the conversion. 0 - out, 1 - in
     *  @return array/any Converted values
     */
    function __formatCol($ColName, &$ColArray, $ConversionType, $Dir) {

        if (in_array($ConversionType,
                     array(SODT_DATE, SODT_TIME, SODT_DATETIME))) {
            $date_format = dzGetIniItem("general", "date", "app");
            $time_format = $this->__Template->getColProperty(
                $ColName, DZCPT_FORMAT, "time");
            if (!$time_format) {
                $time_format = dzGetIniItem("general", "time", "app");
            };
            soConvertDates($ColArray, $ConversionType,
                           $date_format, $time_format, $Dir, 1);
        } else {
            $acount = count($ColArray);
            for ($a = 0; $a < $acount; $a++) {
                switch ($ConversionType) {
                    case DZCV_UPPER:
                        $ColArray[$a] = soCaseConv($ColArray[$a], "U");
                        break;

                    case DZCV_LOWER:
                        $ColArray[$a] = soCaseConv($ColArray[$a], "L");
                        break;

                    case DZCV_BOOL_OUT:
                        $ColArray[$a] = soGetAsType($ColArray[$a], "bool");
                        break;

                    case DZCV_BOOL_IN:
                        $ColArray[$a] = soGetAsType($ColArray[$a], "bool");
                        break;
                    case SODT_DEC:
                    case SODT_INT:
                        if ($Dir) {
                            $ColArray[$a] = str_replace(" ", "",
                                                        trim($ColArray[$a]));
                        } else {
                            $numfmt =
                                $this->__Template->getColProperty(
                                    $ColName, DZCPT_STRUCT, DZCP_NUMFMT);
                            if ($numfmt) {
                                $ColArray[$a] = soNumFmt($ColArray[$a],
                                    intval(soExtractVal($numfmt, ",", 1)),
                                    intval(soExtractVal($numfmt, ",", 2)));
                            };
                        }; // else
                }; // switch
            } // for
        }; // else if date, time or datetime
    } // __formatCol


} // dzFilter

?>
